// 导入数据库操作模块
const db = require('../../db/index')
const { encryption, decrypt } = require('../../utils/password.js')
// 添加管理员
exports.addAdministrator = (req, res) => {
    let { adminId, password, email } = req.body
    const selectSql = `select * from admin where admin_id='${ adminId }'`
    db.query(selectSql, (err, selectRes) => {
        if(err) return res.operationFailed(err)
        if(selectRes.length != 0) return res.operationSuccess('failure', 'ERR020', '该管理员账号已存在！', [])
        let encryptedPwd = encryption(password) // 加密后的密码
        const insertSql = `insert into admin(admin_id, password, a_email) values('${ adminId }', '${ encryptedPwd }', '${ email }')`
        db.query(insertSql, (err, insertRes) => {
            if(err) return res.operationSuccess('failure', 'ERR021', '添加管理员账号失败！', [])
            return res.operationSuccess('success', '', '添加管理员账号成功！', [])
        })
    })
}